package com.gpb.common.transaction;

import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;


public class TransactionManager implements Transaction {
	DataSourceTransactionManager transactionManager;
	TransactionStatus tstatus;


	public DataSourceTransactionManager getTransactionManager() {
		return transactionManager;
	}


	public void setTransactionManager(
			DataSourceTransactionManager transactionManager) {
		this.transactionManager = transactionManager;
	}
	

	public void begin(){
		DefaultTransactionDefinition def = new DefaultTransactionDefinition(); 
		def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); //事物隔离级别，开启新事务，与A类和B类不使用同一个事务。
		tstatus = transactionManager.getTransaction(def); //获得事务状态
	}
	
	public void begin(int ransactionDefinition ){
		DefaultTransactionDefinition def = new DefaultTransactionDefinition(); 
		def.setPropagationBehavior(ransactionDefinition); //事物隔离级别，开启新事务，与A类和B类不使用同一个事务。
		tstatus = transactionManager.getTransaction(def); //获得事务状态
	}
	
	public void commit(){
		transactionManager.commit(tstatus);
	}


	public void rollback(){
		transactionManager.rollback(tstatus);
	}
}
